﻿// Copyright (c) 2012 - 2020 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2018-12-05 version 3.1.5 Jared Zheng <jaredz at outlook dot com>

#include "stdafx.h"
#include "ServerConsole.h"
#include "GateHandler.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// CGateHandler
CGateHandler::CGateHandler(void)
: m_uGateOnline(0)
, m_uGateTotal(0)
, m_uGateLoad(0)
, m_uMasterId(0)
{
}

CGateHandler::~CGateHandler(void)
{
}

void CGateHandler::OnLive(bool bStart)
{
    CStringFix strTemp = GetName();
    if (bStart)
    {
        strTemp += TF("Start---");

        CStrAddr strAddr;
        GServerConsoleInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_GATE, CServerConfig::CFG_LOAD_NONE, strAddr);
        strTemp.AppendFormat(TF("TCP-UDP Ext-Listen=[%s]:%d"), *strAddr.strIp, strAddr.usPort);
    }
    else
    {
        strTemp += TF("Stop");

        m_uGateOnline = 0;
        m_uGateTotal  = 0;
        m_uGateLoad   = 0;
        m_uMasterId   = 0;
    }
    LOG_INFO(GServerConsoleInst->m_FileLog, *strTemp);
}

void CGateHandler::OnLink(Int nServerIndex, uintptr_t utData)
{
    CStringFix strTemp = GetName();
    if (nServerIndex == PAKT_MASTER)
    {
        strTemp += TF("TCP Int-Connect Master=");

        CNetAddr* pAddr = (reinterpret_cast<CNetAddr*>(utData));

        CStrAddr strAddr;

        GServerConsoleInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_MASTER, CServerConfig::CFG_LOAD_NONE, strAddr);
        strTemp.AppendFormat(TF("[%s]:%d"), *strAddr.strIp, strAddr.usPort);

        GServerConsoleInst->m_NetworkPtr->TranslateAddr(strAddr.strIp, strAddr.usPort, *pAddr, false);
        strTemp.AppendFormat(TF("([%s]:%d)"), *strAddr.strIp, strAddr.usPort);
    }
    else if (nServerIndex == PAKT_GATE)
    {
        strTemp += TF("UDP Int-Listen Author=");

        CNetAddr* pAddr = (reinterpret_cast<CNetAddr*>(utData));

        CStrAddr strAddr;
        GServerConsoleInst->m_NetworkPtr->TranslateAddr(strAddr.strIp, strAddr.usPort, *pAddr, false);
        strTemp.AppendFormat(TF("[%s]:%d"), *strAddr.strIp, strAddr.usPort);
    }
    else if (nServerIndex == PAKT_SERVICE)
    {
        strTemp.Format(TF("Online RID=%llX"), utData);
    }
    LOG_INFO(GServerConsoleInst->m_FileLog, *strTemp);
}

void CGateHandler::OnUpdate(Int nCount, uintptr_t utData)
{
    CStringFix strTemp;

    PULLong pData = reinterpret_cast<PULLong>(utData);
    for (Int i = 0; i < nCount; ++i)
    {
        strTemp.Format(TF("Online RID=%llX REQ=%lld, ACK=%lld"), pData[0], pData[1], pData[2]);
        LOG_INFO(GServerConsoleInst->m_FileLog, *strTemp);

        pData += 3;
    }
}

void CGateHandler::OnSync(Int nServerIndex, uintptr_t utData)
{
    if (nServerIndex == PAKT_GATE)
    {
        PSERVER_STATUS pStatus = reinterpret_cast<PSERVER_STATUS>(utData);
        m_uGateTotal  = pStatus->uTotal;
        m_uGateOnline = pStatus->uOnline;
        m_uGateLoad   = pStatus->usLoad / DATAD_TOPERCENT;
    }
    else
    {
        assert(nServerIndex == PAKT_MASTER);
        m_uMasterId   = (UInt)utData;
    }
}

void CGateHandler::OnSync(Int, CStream&)
{
}

void CGateHandler::OnUnlink(Int nServerIndex, uintptr_t utData)
{
    UNREFERENCED_PARAMETER(utData);
    if (nServerIndex == PAKT_MASTER)
    {
        LOG_INFO(GServerConsoleInst->m_FileLog, TF("Gate:TCP Int-Connect Master Closed"));
    }
    else if (nServerIndex == PAKT_AUTHOR)
    {
        LOG_INFO(GServerConsoleInst->m_FileLog, TF("Gate:UDP Int-Listen Author Closed"));
    }
    else if (nServerIndex == PAKT_SERVICE)
    {
        if (utData > 0)
        {
            LOGV_INFO(GServerConsoleInst->m_FileLog, TF("Online RID=%lld offline"), utData);
        }
        else
        {
            LOG_INFO(GServerConsoleInst->m_FileLog, TF("All Online RID offline"));
        }
    }
}

void CGateHandler::UpdateServerInfo(void)
{
    CStringFix strTemp;
    strTemp.Format(TF("[%u/%u, Load=%d%%]Connected-Master Id:%d"),
                   m_uGateOnline, m_uGateTotal, m_uGateLoad, m_uMasterId);
    LOG_INFO(GServerConsoleInst->m_FileLog, *strTemp);
}

PCXStr CGateHandler::GetName(void)
{
    return TF("Gate:");
}




